import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/recipe/data/api/category_recipe_list_api.dart';

import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';
import 'package:casdon/modules/recipe/providers/recipe_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CategoryRecipeListUsecase {
  final RecipeRepository _repository;

  const CategoryRecipeListUsecase(this._repository);

  Future<CategoryRecipeListApi> execute({
    required String id,
    required int pageSize,
    required int pageNum,
  }) async {
    try {
      var ret = await _repository.getCategoryRecipeList(id, pageNum, pageSize);
      return ret ?? CategoryRecipeListApi(total: 0, rows: []);
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("CategoryRecipeListUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("CategoryRecipeListUsecase 失败");
    }
  }
}

final categoryRecipeListUsecaseProvider = Provider<CategoryRecipeListUsecase>((
  ref,
) {
  return CategoryRecipeListUsecase(ref.read(recipeRepository));
});
